/*
 * This file is part of algowala.
 *
 * algowala is copyright (c) 2012 Jagannathan Sampath <algowala@jagernot.com>
 *
 * algowala is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * algowala is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with algowala.  If not, see <http://www.gnu.org/licenses/>.
 *
*/
#ifndef __basic_viewer
#define __basic_viewer

#include "viewwin.h"
#include "point.h"
#include "ui.h"
#include "algo_gl.h"

#include <list>

#include <map>

#include <boost/date_time/posix_time/posix_time.hpp>


struct basic_viewer : ui {
  
  std::string fname; // settings file name
	
  window win; // edit window

  // object space <> window space mapping
  point<int> win_chunk;
  point<double> obj_chunk;
  point<double> win_per_obj;
  point<double> obj_per_win;
    
  // snapping support
  int snap_what;
  enum {SNAP_NONE = 0, SNAP_X, SNAP_Y, SNAP_BOTH};
  void snap (double& x, double& y);
  int is_snapx ();
  int is_snapy ();
  std::vector<int> xlines, ylines;
  int nxpts, nypts;

  basic_viewer (const std::string& settingsf);
  basic_viewer ();
  virtual ~basic_viewer ();

  void obj2win (const point<double>& p, double& wx, double& wy);
  void obj2win (const double& ox, const double& oy, double& wx, double& wy);
  void win2obj (const double& dx, const double& dy, double& cx, double& cy);

  void calc_win_mouse ();
  void calc_visual_params ();
  void calc_win_obj ();
  void calc_snaps (int what);

  void load (const std::string& fname);
  void load (std::ifstream& file);
  void save ();
  void save (std::ofstream& file);

  void set_win_chunk (int x, int y);
  
  bool handle_input ();

  void project ();
  void unproject ();

  void draw ();
  void draw_cursor (const std::string& fmt, const std::string& str);
  void draw_snaps ();
  
  int startx, starty;
  int endx, endy;
  void label_y (const std::string& format);
  
  int timelen;
  void label_x (const boost::posix_time::ptime& startt, double ox_mult = 1);
  
  static const int buf_size = 1024;
  char buf [buf_size];
  
  GLuint gl_prices, gl_times;
  int make_price_list, make_time_list;
  
  void hilite_x (double x);
  void hilite_y (double y);
      
};

#endif
